package main

import (
	"fmt"
	"time"
)

func main() {
	timeDemo()
	timestamp()
	var timeStamp1 int64 = 1748502009
	transformByTimeStamp(timeStamp1)
	timeOperation()
	timeTicker()
	timeFormat()
	parseString()
}

// date
func timeDemo() {
	now := time.Now()
	fmt.Println(now) // 2025-05-29 14:54:34.135601 +0800 CST m=+0.000098389

	year := now.Year()
	month := now.Month()
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute()
	second := now.Second()
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
}

// timestamp
func timestamp() {
	now := time.Now()
	fmt.Println(now.Unix())       // 时间戳 秒 1748501674
	fmt.Println(now.UnixMilli())  //
	fmt.Println(now.UnixMicro())  //
	fmt.Println(now.UnixNano())   // 纳秒时间戳 1748501674135601000
	fmt.Println(now.Nanosecond()) //
}

func transformByTimeStamp(timeStamp int64) {
	timeObj := time.Unix(timeStamp, 0) // 时间戳转日期
	fmt.Println(timeObj)
	year := timeObj.Year()
	month := timeObj.Month()
	day := timeObj.Day()
	hour := timeObj.Hour()
	minute := timeObj.Minute()
	second := timeObj.Second()
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
	fmt.Println(timeObj.Format("2006-01-02 15:04:05"))
}

const (
	Nanosecond  time.Duration = 1
	Microsecond               = 1000 * Nanosecond
	Millisecond               = 1000 * Microsecond
	Second                    = 1000 * Millisecond
	Minute                    = 60 * Second
	Hour                      = 60 * Minute
)

// 时间操作
func timeOperation() {
	fmt.Println("--timeOperation--")
	now := time.Now()
	// add 加一个小时
	oneHourLater := now.Add(time.Hour)
	fmt.Println(oneHourLater.Format("2006-01-02 15:04:05"))
	// add 减一个小时
	oneHourBefore := now.Add(-1 * time.Hour)
	fmt.Println(oneHourBefore.Format("2006-01-02 15:04:05"))
	// AddDate  年月日加减
	nextMonth := now.AddDate(0, 1, 0)
	fmt.Println(nextMonth.Format("2006-01-02 15:04:05"))
	//Sub 求两个时间之间的差值：
	subRes := oneHourBefore.Sub(oneHourLater)
	fmt.Println(subRes) // -2h0m0s
	// Equal
	equalRes := oneHourBefore.Equal(oneHourLater)
	fmt.Println(equalRes) // false
	// Before t在 u 之前
	beforeRes := oneHourBefore.Before(oneHourLater)
	fmt.Println(beforeRes) // true
	// After t在 u 之后
	afterRes := oneHourBefore.After(oneHourLater)
	fmt.Println(afterRes) // false
}

// timeTicker 定时器
func timeTicker() {
	fmt.Println("--timeTicker--")
	ticker := time.Tick(time.Second)
	var x int
	for i := range ticker {
		x++
		fmt.Println(i) //每秒都会执行的任务
		if x > 6 {
			break
		}
	}
}

func timeFormat() {
	now := time.Now()
	// 24小时制
	fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan"))
	// 12小时制
	fmt.Println(now.Format("2006-01-02 03:04:05.000 PM Mon Jan"))
	fmt.Println(now.Format("2006/01/02 15:04"))
	fmt.Println(now.Format("15:04 2006/01/02"))
	fmt.Println(now.Format("2006/01/02"))
}

// 解析字符串格式的时间
func parseString() {
	now := time.Now()
	// 加载时区
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Println(err)
	}
	// 按照指定时区和指定格式解析字符串时间
	timeObj, err := time.ParseInLocation("2006-01-02 15:04:05", "2026-08-01 13:45:06", loc)
	if err != nil {
		return
	}
	fmt.Println(timeObj)
	fmt.Println(timeObj.Sub(now)) // 10293h34m15.501042s
}
